package cn.retalsystem.service.impl;

import cn.retalsystem.bean.Buding;
import cn.retalsystem.bean.Chewei;
import cn.retalsystem.bean.User;
import cn.retalsystem.dao.BudingMapper;
import cn.retalsystem.dao.CheweiMapper;
import cn.retalsystem.dao.UserMapper;
import cn.retalsystem.service.CheweiService;
import cn.retalsystem.vo.CheweiVo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Author: 陈妙斌
 * @Date: 2019/10/31 16:15
 * @Version 1.0
 * @描述：
 */
@Service
public class CheweiServiceImpl implements CheweiService {
    @Autowired
    private CheweiMapper cheweiMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    BudingMapper budingMapper;

//    @Override
//    public List<CheweiVo> gettable(Integer page, Integer limit) {
//        PageHelper.startPage(page,limit);
//        List<CheweiVo> cheweiVos= cheweiMapper.selectAllChewei();
//        for (CheweiVo cheweiVo : cheweiVos) {
//            //取出用户id
//            Integer userId = cheweiVo.getUserId();
//            //获得楼栋id
//            Integer buding = cheweiVo.getBudingId();
//            //根据用户id查询用户
//            User user = userMapper.selectByPrimaryKey(userId);
//            //根据楼栋id查询楼栋
//            Buding buding1 = budingMapper.selectByPrimaryKey(buding);
//            //根据查询到的用户名字给vo类user
//            cheweiVo.setUser(user.getUsername());
//            //根据查询到的楼栋名字给vo类buding
//            cheweiVo.setBuding(buding1.getName());
//            //日期格式化
//            DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
//            String format=dateFormat.format(cheweiVo.getStartday());
//            cheweiVo.setSday(format);
//            DateFormat dateFormat1=new SimpleDateFormat("yyyy-MM-dd");
//            String forma1=dateFormat.format(cheweiVo.getEndday());
//            cheweiVo.setEday(forma1);
//            Integer type = cheweiVo.getTypeId();
//            if (type==0){
//                cheweiVo.setType("固定车位");
//            }else {
//                cheweiVo.setType("临时车位");
//            }
//
//        }
//        return cheweiVos;
//
//    }

    @Override
    public List<CheweiVo> huoQu(Integer page, Integer limit, String bname, Integer type) {
        PageHelper.startPage(page,limit);
        List<CheweiVo> selectroomzl = cheweiMapper.selectcheweiBybudingId(bname,type); //获取值
        return selectroomzl;
    }

    @Override
    public int hcconut(String bname, Integer type) {
        int size = cheweiMapper.selectcheweiBybudingId(bname,type).size();
        return size;
    }

    @Override
    public void deltableinfo(Integer cwid) {
      cheweiMapper.deleteByPrimaryKey(cwid);
    }

    @Override
    public void delCheweiById(List<Integer> ids) {
        for (Integer id : ids) {
            //删除租赁信息
          cheweiMapper.deleteByPrimaryKey(id);


        }
    }

    @Override
    public List<CheweiVo> selectCheweiByExample(Integer page, Integer limit,String bname, Integer type) {
        List<CheweiVo> cheweiVos = cheweiMapper.selectcheweiBybudingId(bname,type);
        for (CheweiVo cheweiVo : cheweiVos) {
            //取出用户id
            Integer userId = cheweiVo.getUserId();
            //获得楼栋id
            Integer buding = cheweiVo.getBudingId();
            //根据用户id查询用户
            User user = userMapper.selectByPrimaryKey(userId);
            //根据楼栋id查询楼栋
            Buding buding1 = budingMapper.selectByPrimaryKey(buding);
            //根据查询到的用户名字给vo类user
            cheweiVo.setUser(user.getUsername());
            //根据查询到的楼栋名字给vo类buding
            cheweiVo.setBuding(buding1.getName());
            //日期格式化
            DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
            String format=dateFormat.format(cheweiVo.getStartday());
            cheweiVo.setSday(format);
            DateFormat dateFormat1=new SimpleDateFormat("yyyy-MM-dd");
            String forma1=dateFormat.format(cheweiVo.getEndday());
            cheweiVo.setEday(forma1);
            Integer typeId = cheweiVo.getTypeId();
            if (typeId==0){
                cheweiVo.setType("固定车位");
            }else {
                cheweiVo.setType("临时车位");
            }
            List<CheweiVo> cheweiVos1 = cheweiMapper.selectAllChewei();
            for (CheweiVo c:cheweiVos1
            ) {
                String phone=c.getPhone();
              cheweiVo.setPhone(phone);
            }

        }

        return cheweiVos;


    }

    @Override
    public Chewei selectCheweiById(Integer id) {
        return cheweiMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateChewei(Chewei chewei) {
        cheweiMapper.updateByPrimaryKeySelective(chewei);
    }

    @Override
    public void addChewei(Chewei chewei,Integer adminId) {

        cheweiMapper.insertSelective(chewei);

        Integer cheweiId = chewei.getId();
//       List<CheweiVo> cheweis=cheweiMapper.selectAllChewei();
//        for (CheweiVo c:cheweis
//             ) {
//            chewei.setId(c.getCheweiID());
//            chewei.setType(c.getTypeId());
//            chewei.setBudingid(c.getBudingId());
//            chewei.setUserid(c.getUserId());
//            chewei.setCartype(c.getCartype());
//            chewei.setNumber(c.getCarnumber());
//        }


        
    }

}
